package p.service;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ac_service extends Activity {
	
    TextView textview;
    Button button_start;
    Button button_stop;
    DataReceiver datareceiver;
    
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        button_start = (Button)findViewById(R.id.button_start);
        button_stop = (Button)findViewById(R.id.button_stop);
        textview = (TextView)findViewById(R.id.textview_state);
        button_start.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(ac_service.this, p.service.myservice.class);
				startService(intent);
			}
		});
        
        button_stop.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setAction("p.service.myservice");
				sendBroadcast(intent);
				/*intent.setClass(ac_service.this, p.service.myservice.class);
				stopService(intent);*/
			}
		});
        
        
        
    }
    
    @Override
	protected void onStart() {
		// TODO Auto-generated method stub
    	System.out.print("this is ac_service -----> onStart()");
    	datareceiver = new DataReceiver();
    	IntentFilter intentfilter = new IntentFilter();
    	intentfilter.addAction("p.service.ac_service");
    	registerReceiver(datareceiver, intentfilter);
		super.onStart();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	private class DataReceiver extends BroadcastReceiver{

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			double random = intent.getDoubleExtra("random_num", 0);
			textview.setText("random_num:" + random);
		}
    	
    }
}